<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class WdminAjax extends Controller {

    const groupSendImageField = 'fileselect';

    public function __construct($ControllerName, $Action, $QueryString) {
        parent::__construct($ControllerName, $Action, $QueryString);
        $this->loadModel('Db');
    }

    /**
     * groupSendImageUpload
     */
    public function groupSendImageUpload($Query) {
        global $config;
        !isset($Query->name) && $Query->name = '';
        if (!empty($GLOBALS["HTTP_RAW_POST_DATA"])) {
            $dirname = "/static/groupsend_upload/";
            $targetPath = dirname(__FILE__) . "/../static/groupsend_upload/";
            $tempFile = $_FILES[self::groupSendImageField]['tmp_name'];
            $targetFileName = sha1(time()) . '-' . $Query->name;
            $targetFile = str_replace('//', '/', $targetPath) . $targetFileName;
            $state = file_put_contents($targetFile, $GLOBALS["HTTP_RAW_POST_DATA"]) > 0 ? 1 : 0;
        } else {
            $state = 0;
        }
        $this->echoJson(array(
            "status" => $state,
            "imagepath" => urlencode("http://" . $_SERVER['HTTP_HOST'] . $config->shoproot . 'static/groupsend_upload/' . $targetFileName),
            "imagename" => $targetFileName
        ));
    }

    /**
     * 
     * @param type $Query
     */
    public function createGmessPage() {
        global $config;
        $SQL = sprintf("INSERT INTO `gmess_page` (`title`,`content`,`desc`,`catimg`,`createtime`,`appid`)"
                . " VALUES ('%s','%s','%s','%s',NOW(),'%s');", $_POST['title'], $_POST['content'], $_POST['desc'], urldecode($_POST['catimg']), APPID);
        $rst = $this->Db->query($SQL);
        echo $rst ? "http://" . $_SERVER['HTTP_HOST'] . "$config->shoproot?/Gmess/view/id=" . $rst : 0;
    }

    public function ajaxGetSubScribelist() {
        $this->loadModel('WechatSdk');
        $access_token = WechatSdk::getServiceAccessToken();
        $list = WechatSdk::getWechatSubscriberList($access_token);
        $list['accesstoken'] = $access_token;
        $this->echoJson($list);
    }

}
